package com.tingyuge.base.thread.wang.chapter8;

/**
 * Created by xiaodao
 * date: 2019/11/29
 * kill -9 强制杀..
 * kill 会让hook 执行
 */
public class ThreadRuntime {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("程序即将推出");
            nofifyAndRelease();
        }));
        int i =0;
        while (true){
            try {
                Thread.sleep(1000);
                System.out.println("im working");

            } catch (Throwable e) {
                e.printStackTrace();
            }
            i++;
            if(i>20){
                throw new RuntimeException("error");
            }

        }
    }

    private static void nofifyAndRelease() {
        System.out.println("notify to the admin");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("i will release resource");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Release is done");
    }
}
